﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.AssetManage;
using LJX.SmartPark.Domain.SafeManage;
using LJX.SmartPark.Domain.Student;
using LJX.SmartPark.EntityFramework;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.ParkManage
{
    public class VisitorRequestService : IVisitorRequestService
    {

        private readonly IRepository<VisitorRequest> _studentService;

        private readonly StudentServerContext _context;

        public VisitorRequestService(IRepository<VisitorRequest> studentService, StudentServerContext context)
        {
            _studentService = studentService;
            _context = context;
        }

        public async Task<Response<bool>> DeleteVisitorRequest(VisitorRequest input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<VisitorRequest>());
            return Response<bool>.Ok(result > 0);
        }


        public async Task<Response<List<VisitorRequest>>> GetVisitorRequest(VisitorRequestForm form)
        {
            var students = await _studentService.GetAllListAsync();
            if (!string.IsNullOrEmpty(form.name))
            {
                students = students.Where(x => x.VisitorName.Contains(form.name)).ToList();
            }
            if (!string.IsNullOrEmpty(form.bumeng))
            {
                students = students.Where(x => x.DepartVisited.Contains(form.bumeng)).ToList();
            }
            if (form.startdate != null && form.enddate != null)
            {
                students = students.Where(x => x.AccessTime >= form.startdate && x.AccessTime <= form.enddate).ToList();
            }
            var count = students.Count();
            students = students.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            var result = students.MapToList<VisitorRequest>();
            return Response<List<VisitorRequest>>.Ok(result, count);
        }

    }
}
